﻿#------------------------------------------------------------------
#add_version.cmake
#please dont modify this cmake
#2018年7月11日 22点04分
#------------------------------------------------------------------
#ADD_VERSION()
#ADD_TARGET_VERSION()

#为multi-link提供变量PROJECT_VERSION LIB_VERSION APP_VERSION
#为源代码提供PROJECT_VERSION VERSION APP_VERSION LIB_VERSION宏

#cmake，multi-link环境添加对PROJECT_VERSION变量的使用。
#在新版本里面，如果用project定义了版本号，那么使用project设置的这个变量。
IF(POLICY CMP0048)
    CMAKE_POLICY(SET CMP0048 NEW)
ENDIF()

#源代码环境和qmake版multi-link一样。
FUNCTION(ADD_VERSION MAJOR_VER MINOR_VER PATCH_VER BUILD_VER)
    #sub version
    #app major minon patch version
    SET(APP_MAJOR_VERSION ${MAJOR_VER} CACHE INTERNAL "APP_MAJOR_VERSION")
    SET(APP_MINOR_VERSION ${MINOR_VER} CACHE INTERNAL "APP_MINOR_VERSION")
    SET(APP_PATCH_VERSION ${PATCH_VER} CACHE INTERNAL "APP_PATCH_VERSION")
    SET(APP_BUILD_VERSION ${BUILD_VER} CACHE INTERNAL "APP_BUILD_VERSION")

    SET(PROJECT_VERSION_MAJOR ${MAJOR_VER} CACHE INTERNAL "PROJECT_VERSION_MAJOR")
    SET(PROJECT_VERSION_MINOR ${MINOR_VER} CACHE INTERNAL "PROJECT_VERSION_MINOR")
    SET(PROJECT_VERSION_PATCH ${PATCH_VER} CACHE INTERNAL "PROJECT_VERSION_PATCH")
    SET(PROJECT_VERSION_TWEAK ${BUILD_VER} CACHE INTERNAL "PROJECT_VERSION_TWEAK")

    #version
    SET(APP_VERSION1 ${MAJOR_VER})
    SET(APP_VERSION2 ${MAJOR_VER}.${MINOR_VER})
    SET(APP_VERSION3 ${MAJOR_VER}.${MINOR_VER}.${PATCH_VER})
    SET(APP_VERSION4 ${MAJOR_VER}.${MINOR_VER}.${PATCH_VER}.${BUILD_VER})

    SET(VERSION ${APP_VERSION4} CACHE INTERNAL "VERSION")
    SET(APP_VERSION ${APP_VERSION4} CACHE INTERNAL "APP_VERSION")
    SET(LIB_VERSION ${APP_VERSION4} CACHE INTERNAL "LIB_VERSION")
    SET(PROJECT_VERSION ${APP_VERSION4} CACHE INTERNAL "PROJECT_VERSION")

    IF(APP_VERSION1)
        MESSAGE ("${PROJECT_NAME} version: v${APP_VERSION4}")
    ENDIF()

    #version definitions
    REMOVE_DEFINITIONS(-DVERSION=${PRE_VERSION})
    REMOVE_DEFINITIONS(-DLIB_VERSION=${PRE_LIB_VERSION})
    REMOVE_DEFINITIONS(-DAPP_VERSION=${PRE_APP_VERSION})
    REMOVE_DEFINITIONS(-DPROJECT_VERSION=${PRE_PROJECT_VERSION})

    ADD_DEFINITIONS(-DVERSION=${PROJECT_VERSION})
    ADD_DEFINITIONS(-DLIB_VERSION=${PROJECT_VERSION})
    ADD_DEFINITIONS(-DAPP_VERSION=${PROJECT_VERSION})
    ADD_DEFINITIONS(-DPROJECT_VERSION=${PROJECT_VERSION})

    SET(PRE_VERSION ${PROJECT_VERSION} CACHE INTERNAL "PRE_VERSION")
    SET(PRE_LIB_VERSION ${PROJECT_VERSION} CACHE INTERNAL "PRE_LIB_VERSION")
    SET(PRE_APP_VERSION ${PROJECT_VERSION} CACHE INTERNAL "PRE_APP_VERSION")
    SET(PRE_PROJECT_VERSION ${PROJECT_VERSION} CACHE INTERNAL "PRE_PROJECT_VERSION")
ENDFUNCTION()

#添加target的版本。
FUNCTION(ADD_TARGET_VERSION TARGET MAJOR_VER MINOR_VER PATCH_VER BUILD_VER)
    #sub version
    #app major minon patch version
    SET(${TARGET}_MAJOR_VERSION ${MAJOR_VER} CACHE INTERNAL "${TARGET}_MAJOR_VERSION")
    SET(${TARGET}_MINOR_VERSION ${MINOR_VER} CACHE INTERNAL "${TARGET}_MINOR_VERSION")
    SET(${TARGET}_PATCH_VERSION ${PATCH_VER} CACHE INTERNAL "${TARGET}_PATCH_VERSION")
    SET(${TARGET}_BUILD_VERSION ${BUILD_VER} CACHE INTERNAL "${TARGET}_BUILD_VERSION")

    SET(${TARGET}_VERSION_MAJOR ${MAJOR_VER} CACHE INTERNAL "${TARGET}_VERSION_MAJOR")
    SET(${TARGET}_VERSION_MINOR ${MINOR_VER} CACHE INTERNAL "${TARGET}_VERSION_MINOR")
    SET(${TARGET}_VERSION_PATCH ${PATCH_VER} CACHE INTERNAL "${TARGET}_VERSION_PATCH")
    SET(${TARGET}_VERSION_TWEAK ${BUILD_VER} CACHE INTERNAL "${TARGET}_VERSION_TWEAK")

    #version
    SET(APP_VERSION1 ${MAJOR_VER})
    SET(APP_VERSION2 ${MAJOR_VER}.${MINOR_VER})
    SET(APP_VERSION3 ${MAJOR_VER}.${MINOR_VER}.${PATCH_VER})
    SET(APP_VERSION4 ${MAJOR_VER}.${MINOR_VER}.${PATCH_VER}.${BUILD_VER})

    SET(${TARGET}_VERSION ${APP_VERSION4} CACHE INTERNAL "${TARGET}_VERSION")

    IF(APP_VERSION1)
        MESSAGE ("${TARGET} version: v${APP_VERSION4}")
    ENDIF()

    #version definitions
    #cmake这个定义不移除也不会存在，cmake这个宏定义比较干净。
    REMOVE_DEFINITIONS(-D${TARGET}_VERSION=${${TARGET}_PRE_VERSION})
    ADD_DEFINITIONS(-D${TARGET}_VERSION=${${TARGET}_VERSION})

    SET(${TARGET}_PRE_VERSION ${${TARGET}_VERSION} CACHE INTERNAL "${TARGET}_PRE_VERSION")
ENDFUNCTION()
